<?php
require_once dirname(__FILE__) . '/libraries/ServiceBase.php';
/**
 * Integral Service （积分服务）
 */
class SDOServiceIntegral extends ServiceBase
{
    // 正式环境开通积分服务
    const INTEGRAL_OP_URL = 'INTEGRAL_WSDL';
    // 测试环境开通积分服务
    const PRE_INTEGRAL_OP_URL = 'PRE_INTEGRAL_WSDL';
    // 操作默认值
    const OPERATION_DEFAULT = 1;
    // 附加信息默认值
    const APPENDIX_DEFAULT = '';
    // 错误代码，信息
    protected static $errCodeMsg = array(
        - 1 => "游戏不存在" , 
        - 2 => "录入配置信息有误"
    );
    /**
     * 开通SDO积分
     * 
     * @param string $message 错误信息
     * @param int $OAMerchantInfo array(spId => 公司ID
     * appId => 产品ID
     * spName => 公司名称
     * spAddr => 公司地址
     * spProductIntro => 产品说明
     * spTelphone => 公司电话
     * spPhoneNumber => 公司手机(默认空)
     * spContactPeron => 负责人,
     * spEmail => 邮箱
     * )
     * @param string $env 
     * @param int $operation 0 关闭，1开通 
     * @return boolean 
     */
    public static function JoinSNDAJf (&$message, $OAMerchantInfo, $env = 'pre', $operation=self::OPERATION_DEFAULT)
    {
        $need = array(
            'spId' , 
            'appId' , 
            'spName' , 
            'spAddr' , 
            'spTelphone' , 
            'spContactPeron' , 
            'spEmail'
        );
        if (! self::_validateParam($OAMerchantInfo, $need, $message)) {
            return false;
        }
        $op = 'JoinSNDAJf';
        $wsdl = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_INTEGRAL_OP_URL) : sdk_fetch_config(SDK_ENV . self::INTEGRAL_OP_URL);
        //$OAMerchantInfo = iconv_array( $OAMerchantInfo, "UTF-8", "GB2312" );
        if (! isset($OAMerchantInfo['spProductIntro'])) {
            $OAMerchantInfo['spProductIntro'] = '';
        }
        $OAMerchantInfo['spPhoneNumber'] = '';
        $OAMerchantInfo['Remark1'] = $operation;
        $OAMerchantInfo['Remark2'] = self::APPENDIX_DEFAULT;
        $options = array(
            'soap_version' => SOAP_1_1 , 
            'user_agent' => 'chaoguo6wei'
        );
        $param['oaMerchangInfo'] = $OAMerchantInfo;        
        $result = self::soapRequest($op, $param, $wsdl, $options, $message);
        //print_r($OAMerchantInfo);
        //print_r($result);
        if ($result===false) {
        	self::log("method: SetIGWConfig\tparams: ".json_encode($param)."\tenv: {$env}\texception: ".$message, __CLASS__);
        	return false;
        }
        if ($result->JoinSNDAJfResult !== 0) {
            $message = $result->JoinSNDAJfResult ? self::$errCodeMsg[$result->JoinSNDAJfResult]:$message;
            self::log("method: JoinSNDAJf\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__);
            return false;
        }
        self::log("method: JoinSNDAJf\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
        return true;
    }
	/**
     * 自检函数
     */
    public static function selfTest() 
    {
    	$arr = array(
        	sdk_fetch_config(SDK_ENV . self::INTEGRAL_OP_URL),
        	sdk_fetch_config(SDK_ENV . self::PRE_INTEGRAL_OP_URL)
    	);
    	return self::curlTest($arr, array("igw24/JOINSNDAJF.asmx"=>""));
    }
} 
